package model3d {
	import laya.d3.component.Script3D;
	import laya.d3.core.Camera;
	import laya.d3.core.material.RenderState;
	import laya.d3.math.Quaternion;
	import laya.d3.math.Vector3;
	import laya.display.Scene;
	import laya.events.Event;
	import laya.events.KeyBoardManager;
	
	/**
	 * ...
	 * @author
	 */
	public class CameraMoveScript extends Script3D {
		
		protected var lastMouseX:Number;
		protected var lastMouseY:Number;
		protected var yawPitchRoll:Vector3 = new Vector3();
		protected var resultRotation:Quaternion = new Quaternion();
		protected var tempRotationZ:Quaternion = new Quaternion();
		protected var tempRotationX:Quaternion = new Quaternion();
		protected var tempRotationY:Quaternion = new Quaternion();
		protected var isMouseDown:Boolean;
		protected var rotaionSpeed:Number = 0.002;
		protected var camera:Camera;
		protected var scene:Scene;
		private var lastDistance:Number;
		private var rotationV:Quaternion;
		private var positionV:Vector3;
		public function CameraMoveScript() {
			
			
		}
		
		override public function onAwake():void
		{
			// TODO Auto Generated method stub
			super.onAwake();
			camera = owner as Camera;
			rotationV=camera.transform.rotation.clone();
			positionV=camera.transform.position.clone();
			
			//鼠标右键选择相机
			Laya.stage.on(Event.RIGHT_MOUSE_DOWN, this, rightmouseDown);
			Laya.stage.on(Event.RIGHT_MOUSE_UP, this, rightMouseUp);
			
			//鼠标左键多点缩放视角
			Laya.stage.on(Event.MOUSE_DOWN,this,onMouseDown);
			Laya.stage.on(Event.MOUSE_UP, this, onMouseUp);
			Laya.stage.on(Event.MOUSE_OUT, this, onMouseUp);
			
			//鼠标滚轮
			Laya.stage.on(Event.MOUSE_WHEEL,this,mouseWheel);//鼠标滚轮
		}
		
		
		private function onMouseDown(e:Event):void
		{
			// TODO Auto Generated method stub
			var touches:Array = e.touches;
			if(touches && touches.length == 2)
			{
				lastDistance = getDistance(touches);
				Laya.stage.on(Event.MOUSE_MOVE, this, onMouseMove);
			}
		}
		
		private function onMouseUp():void
		{
			// TODO Auto Generated method stub
			isMouseDown = false;
			Laya.stage.off(Event.MOUSE_MOVE, this, onMouseMove);
		}
		
		private function mouseWheel(e:Event):void
		{
			// TODO Auto Generated method stub
			owner.event("FieldOfViewScale",e.delta);
		}
		override public function onUpdate():void{
			owner.event("CAMERA_UPDATE");
			updateCamera();
		}
		
		protected function rightmouseDown(e:Event):void {
			camera.transform.localRotation.getYawPitchRoll(yawPitchRoll);
			lastMouseX = Laya.stage.mouseX;
			lastMouseY = Laya.stage.mouseY;
			isMouseDown = true;
		}
		
		private function onMouseMove(e:Event):void
		{
			// TODO Auto Generated method stub
			var distance:Number = getDistance(e.touches);
			//判断当前距离与上次距离变化，确定是放大还是缩小
			const factor:Number = 0.1;
			owner.event("FieldOfViewScale",(distance - lastDistance) * factor);
			lastDistance = distance;
		}
		/**计算两个触摸点之间的距离*/
		private function getDistance(points:Array):Number
		{
			var distance:Number = 0;
			if (points && points.length == 2)
			{
				var dx:Number = points[0].stageX - points[1].stageX;
				var dy:Number = points[0].stageY - points[1].stageY;
				
				distance = Math.sqrt(dx * dx + dy * dy);
			}
			return distance;
		}
		
		protected function rightMouseUp(e:Event):void {
			isMouseDown = false;
		}
		
		protected function updateCamera():void {
			var elapsedTime:Number=0.2;
			if (!isNaN(lastMouseX) && !isNaN(lastMouseY)) {
				var scene:Scene =(owner as Camera).scene;
				KeyBoardManager.hasKeyDown(87) && camera.transform.translate(new Vector3(0,0,-0.001 * elapsedTime));//W
				KeyBoardManager.hasKeyDown(83) && camera.transform.translate(new Vector3(0,0,0.001 * elapsedTime));//S
				KeyBoardManager.hasKeyDown(65) && camera.transform.translate(new Vector3(-0.001 * elapsedTime,0,0));//A
				KeyBoardManager.hasKeyDown(68) && camera.transform.translate(new Vector3(0.001 * elapsedTime,0,0));//D
				KeyBoardManager.hasKeyDown(81) && camera.transform.translate(new Vector3(0,0.001 * elapsedTime,0));//Q
				KeyBoardManager.hasKeyDown(69) && camera.transform.translate(new Vector3(0,-0.001 * elapsedTime,0));//E
				if (isMouseDown) {
					var offsetX:Number = Laya.stage.mouseX - lastMouseX;
					var offsetY:Number = Laya.stage.mouseY - lastMouseY;
					yawPitchRoll.x -= offsetX * rotaionSpeed * elapsedTime;
					yawPitchRoll.y -= offsetY * rotaionSpeed * elapsedTime;
					updateRotation();
				}
			}
			lastMouseX = Laya.stage.mouseX;
			lastMouseY = Laya.stage.mouseY;
			
		}
		protected function updateRotation():void {
			Quaternion.createFromYawPitchRoll(yawPitchRoll.x, yawPitchRoll.y, yawPitchRoll.z, tempRotationZ);
			tempRotationZ.cloneTo(camera.transform.localRotation);
			camera.transform.localRotation = camera.transform.localRotation;
		}
		override public function onDisable():void
		{
			// TODO Auto Generated method stub
			super.onDisable();
			Laya.stage.off(Event.MOUSE_DOWN, this, rightmouseDown);
			Laya.stage.off(Event.MOUSE_UP, this, rightMouseUp);
			Laya.stage.off(Event.MOUSE_OUT, this, onMouseUp);
			Laya.stage.off(Event.MOUSE_DOWN, this, onMouseDown);
			Laya.stage.off(Event.MOUSE_WHEEL,this,mouseWheel);//鼠标滚轮
		}
		//初始化
		public function reset():void
		{
			// TODO Auto Generated method stub
			camera.transform.rotation=rotationV.clone();
			camera.transform.position=positionV.clone();
		}
	}
	
}